import 'package:flutter/material.dart';
// import 'package:flutter_lagou/pages/home/HomeSwiper.dart';
import '../../utils/Global.dart';
import 'HomeSwiper.dart';
import 'HomeCourse.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class Home extends StatefulWidget {
  const Home({Key? key}):super(key: key);
  @override
  State<Home> createState() => _HomeState();
}
// 1.需要声明混入
class _HomeState extends State<Home> with AutomaticKeepAliveClientMixin {
  List adList = [];
  List courseList = [];
  @override
  void initState() {
    // 请求广告
    G.api.ad.adList().then((res) {
      setState(() {
        adList = res.where((course) => course['status'] == 1).toList();
    
      });
    });
    // 请求课程
    G.api.course.courseList().then((res) {
      // print(res);
      setState(() {
        courseList = res;
      });
    });
    super.initState();

  }
  @override
  Widget build(BuildContext context) {
    // 3.调用父类的build
    super.build(context);
    return Container(
      width: double.infinity,
      color: Colors.grey[200],
      child: CustomScrollView(
        slivers: [
          // 轮播图
          SliverToBoxAdapter(
            child: Container(
              height: 300.h,
              child: HomeSwiper(adList: adList),
            ),
          ),
          SliverPadding(
            padding: EdgeInsets.all(5),
            sliver: HomeCourse(courseList: courseList),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
  
  // 2.声明需要缓存
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}